package com.example.weixin_01.services;

import com.example.weixin_01.mapper.StudentMapper;
import com.example.weixin_01.pojo.Student;
import com.example.weixin_01.pojo.StudentDto;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.Random;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Author: zhenghuisheng
 * @Date: 2022/8/12 22:22
 */
@Service
public class StudentServiceImpl implements StudentServiceI {
    @Autowired
    StudentMapper studentMapper;

    /**
     * 对象之间的copy，返回一个可用的dto
     * @return
     */
    @Override
    public List<StudentDto> getAllStudent() {
        List<Student> stu = studentMapper.getAllStudent();
        List<StudentDto> copyList = stu.stream().map(student -> {
            StudentDto studentDto = new StudentDto();
            studentDto.setScore(new Random().nextInt(100));
            BeanUtils.copyProperties(student, studentDto);
            return studentDto;
        }).collect(Collectors.toList());
        return copyList;
    }

    /**
     * 过滤，去重，跳过，limit分页
     * 如果是对象去重，那么需要这个对象的hashcode相同和equals相同
     */
    @Override
    public List<Student> toFilter() {
        List<Student> allStudent = studentMapper.getAllStudent();
        List<Student> filterList = allStudent.stream()
                .filter(student -> student.getAge() > 19)
                .distinct()
                .skip(1)
                .limit(2)
                .collect(Collectors.toList()); //流转化为这个集合
        return filterList;
    }

    /**
     * 排序
     */
    @Override
    public List<Student> toSort() {
        List<Student> allStudent = studentMapper.getAllStudent();
        List<Student> sortList = allStudent.stream().sorted(
                new Comparator<Student>() {
                    @Override
                    public int compare(Student o1, Student o2) {
                        return o1.getAge() - o2.getAge();
                    }
                }
        ).collect(Collectors.toList());
        return sortList;
    }

    /**
     * 获取年龄最大或者最小的学生
     */
    @Override
    public Student toGetMaxOrMin() {
        List<Student> allStudent = studentMapper.getAllStudent();
        //获取最大值
        Optional<Student> max = allStudent.stream().max(
                new Comparator<Student>() {
                    @Override
                    public int compare(Student o1, Student o2) {
                        return o1.getAge() - o2.getAge();
                    }
                }
        );
        return max.get();
    }


    /**
     * 获取第一个学生，或者任意一个学生
     */
    @Override
    public Student toGetOneStudent() {
        List<Student> allStudent = studentMapper.getAllStudent();
        //第一个
        Optional<Student> first = allStudent.stream().findFirst();
        //任意一个
        //Optional<Student> any = allStudent.stream().findAny();
        return first.get();
    }

    /**
     * 循环遍历每个学生
     */
    @Override
    public void foreachPrint() {
        List<Student> allStudent = studentMapper.getAllStudent();
        allStudent.stream().forEach(e-> System.out.println(e));
    }




}

